Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Force interpolation of single quoted character

Reply
Thread Tools

Force interpolation of single quoted character

 
 
Scott Bass
Guest
Posts: n/a
 
      05-27-2005
Hi,

use Getopt::Long;
GetOptions (
"sep:s" => \$sep
);
$sep = "$sep"; # doesn't work
print "line1",$sep,"line2";

If I invoke this as --sep \n or --sep \t, how can I force the interpolation
of the escapse sequence?

Thanks,
Scott


 
Reply With Quote
 
 
 
 
Shawn Corey
Guest
Posts: n/a
 
      05-27-2005
Scott Bass wrote:
> Hi,
>
> use Getopt::Long;
> GetOptions (
> "sep:s" => \$sep
> );
> $sep = "$sep"; # doesn't work
> print "line1",$sep,"line2";
>
> If I invoke this as --sep \n or --sep \t, how can I force the interpolation
> of the escapse sequence?
>
> Thanks,
> Scott
>
>


eval '$sep = "$sep";';

--- Shawn
 
Reply With Quote
 
 
 
 
Scott Bass
Guest
Posts: n/a
 
      05-27-2005

"Shawn Corey" <> wrote in message
news:CSDle.23740$ ...
> Scott Bass wrote:
>> Hi,
>>
>> use Getopt::Long;
>> GetOptions (
>> "sep:s" => \$sep
>> );
>> $sep = "$sep"; # doesn't work
>> print "line1",$sep,"line2";
>>
>> If I invoke this as --sep \n or --sep \t, how can I force the
>> interpolation of the escapse sequence?
>>
>> Thanks,
>> Scott

>
> eval '$sep = "$sep";';
>
> --- Shawn


test1.pl:

use Getopt::Long;
GetOptions (
"sep:s" => \$sep
);
eval '$sep = "$sep";';
print "line1",$sep,"line2";

C:\> test.pl -sep "\n"

gives me

line1\nline2

BTW, ActiveState Perl on Windows - wouldn't think that matters though.


 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-27-2005


Shawn Corey wrote:
> Scott Bass wrote:
>
>> Hi,
>>
>> use Getopt::Long;
>> GetOptions (
>> "sep:s" => \$sep
>> );
>> $sep = "$sep"; # doesn't work
>> print "line1",$sep,"line2";
>>
>> If I invoke this as --sep \n or --sep \t, how can I force the
>> interpolation of the escapse sequence?


This is of course one FAQ.

And very similar to another one.

And as regular readers will know I find the fact that the answers given
in the FAQ pretends the simple/obvious/direct answer does not exist a
constant source of annoyance.

I even gave a lightning talk at YAPC::Europe::2004 about it.

This is because it means that people get shown (or figure out for
themselves) the broken form of the obvious answer...

> eval '$sep = "$sep";';


.... yes, that's the one. That breaks if $sep contains double quote
characters.

The better version of that is...

chop( $sep = eval "<<__EOD__\n$sep\n__EOD__");
do_something if $@;

That only breaks if $sep contains the sequence "\n__EOD__\n" which is
much less likely to occur in the normal course of events.


And also by excuding this "dangerous knowledge" from the FAQ the real
effect is that people are simply are not warned of the security dangers
of using eval(STING) on data that could come from someone who doesn't
have a ligitmate way of getting full shell access (under the current
effective UID).

Note: for expanding escapes rather than interpolating variables the
solution in the FAQ is probably better anyhow.

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-27-2005


Brian McCauley wrote:

> the broken form of the obvious answer...
>
>> eval '$sep = "$sep";';

>
> ... yes, that's the one.


Oops, no it's not even that.

The 'correct' broken form is...

eval qq'$sep = "$sep";';

But it's still better to use the less broken form, or better still the
FAQ solution.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-27-2005
Scott Bass <> wrote:
> Hi,
>
> use Getopt::Long;
> GetOptions (
> "sep:s" => \$sep
> );
> $sep = "$sep"; # doesn't work
> print "line1",$sep,"line2";
>
> If I invoke this as --sep \n or --sep \t,



Then you would have $sep eq 'n' or $sep eq 't', so I'll assume that
you meant

--sep '\n'

instead.


> how can I force the interpolation
> of the escapse sequence?



I would solve the problem without the need for forcing interpolation.

------------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;

our $sep;
GetOptions (
"sep:s" => \$sep
);

my %separators = (
'\n' => "\n",
'\t' => "\t"
);
die "'$sep' is not a valid separator\n" unless $sep =~ /^(\\[tn])$/;

$sep = $separators{$sep};
print "line1",$sep,"line2";
print "\n";
------------------------------


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Scott Bass
Guest
Posts: n/a
 
      05-27-2005
"Tad McClellan" <> wrote in message
news:...
> Scott Bass <> wrote:
>> Hi,
>>
>> use Getopt::Long;
>> GetOptions (
>> "sep:s" => \$sep
>> );
>> $sep = "$sep"; # doesn't work
>> print "line1",$sep,"line2";
>>
>> If I invoke this as --sep \n or --sep \t,

>
>
> Then you would have $sep eq 'n' or $sep eq 't', so I'll assume that
> you meant
>
> --sep '\n'
>
> instead.


Yes, command line options of -sep \n, -sep \t, etc. However, -sep \n, -sep
"\n", or -sep '\n' all seem to give the same results after processing by
GetOptions.

>> how can I force the interpolation
>> of the escapse sequence?

>
>
> I would solve the problem without the need for forcing interpolation.
>
> ------------------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Getopt::Long;
>
> our $sep;
> GetOptions (
> "sep:s" => \$sep
> );
>
> my %separators = (
> '\n' => "\n",
> '\t' => "\t"
> );
> die "'$sep' is not a valid separator\n" unless $sep =~ /^(\\[tn])$/;
>
> $sep = $separators{$sep};
> print "line1",$sep,"line2";
> print "\n";


Sorry I oversimplied my post for the purpose of brevity. I'd like the users
of my script to be able to specify, for example:

-sep \n=====\n
-sep \n\n\n\
-sep \t
-sep #

etc.

Brian, based on your replies, I skimmed all the FAQ's, esp. perlfaq4. Can I
have a hint as to which FAQ this is explained?

> ------------------------------
>
>
> --
> Tad McClellan SGML consulting
> Perl programming
> Fort Worth, Texas



 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-27-2005
Scott Bass wrote:

> "Tad McClellan" <> wrote in message
> news:...


[snip Tad ]

> Brian,


It would keep the thread cleaner if you were to post the follow-up to me
as a follow-up to me.

> based on your replies, I skimmed all the FAQ's, esp. perlfaq4. Can I
> have a hint as to which FAQ this is explained?


The question you are asking _is_ frequently asked, but I think I'd
confused myself and then in turn you too.

The FAQ "How do I unescape a string?" does _not_ answer your question.
Nor does the FAQ "How can I expand variables in text strings?".

Both of these _should_ IMNSHO mention the chop-eval-heredoc solution if
only so that they can explain when it is unsafe to use. I have in the
past battled with FAQ maintainers on this issue but never got very far.

I have to confess that I mis-remebered the FAQ "How do I unescape a
string?" as giving Tad's solution. May I be placed in virtual stocks
and pelted with virtual rotten fruit for not checking my facts before I
posted.
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-27-2005


Brian McCauley wrote:

> Both of these _should_ IMNSHO mention the chop-eval-heredoc solution if
> only so that they can explain when it is unsafe to use.


BTW: here's a slightly safer eval based solution

s/(\\[^"\$\@]+)/qq("$1")/eeg;

Note that's not 100% infalible but AFAIK it's not a security problem and
copes with all reasonable strings. It does have the interesting side
effect of stripping \$ \" and \@ - but those have no buisness being in
the strings you are talking about.
 
Reply With Quote
 
Scott Bass
Guest
Posts: n/a
 
      05-29-2005
"Brian McCauley" <> wrote in message
news:d77pkf$473$...
> Scott Bass wrote:
>
>> "Tad McClellan" <> wrote in message
>> news:...

>
> [snip Tad ]
>
> [snip Brian]
>
> The question you are asking _is_ frequently asked, but I think I'd
> confused myself and then in turn you too.
>
> The FAQ "How do I unescape a string?" does _not_ answer your question. Nor
> does the FAQ "How can I expand variables in text strings?".
>
> Both of these _should_ IMNSHO mention the chop-eval-heredoc solution if
> only so that they can explain when it is unsafe to use. I have in the
> past battled with FAQ maintainers on this issue but never got very far.
>
> I have to confess that I mis-remebered the FAQ "How do I unescape a
> string?" as giving Tad's solution. May I be placed in virtual stocks and
> pelted with virtual rotten fruit for not checking my facts before I
> posted.


Hey, happens to the best of us I've even been known to do it on rare
occasions.

Summary of Brian's previous posts:

> The better version of that is...
>
> chop( $sep = eval "<<__EOD__\n$sep\n__EOD__");
> do_something if $@;
>
> That only breaks if $sep contains the sequence "\n__EOD__\n" which is much
> less likely to occur in the normal course of events.


and

> BTW: here's a slightly safer eval based solution


I assume that should be "non eval based solution"?

>
> s/(\\[^"\$\@]+)/qq("$1")/eeg;
>
> Note that's not 100% infalible but AFAIK it's not a security problem and
> copes with all reasonable strings. It does have the interesting side
> effect of stripping \$ \" and \@ - but those have no buisness being in the
> strings you are talking about.


Thanks for the help Brian. In my final test program:

use Getopt::Long;
GetOptions (
"sep:s" => \$sep
);
#chop($sep = eval "<<__$$__\n$sep\n__$$__");
$sep =~ s/(\\[^"\$\@]+)/qq("$1")/eeg;
print join $sep, ("line1","line2");

both solutions work a treat. I'll go with the here doc solution; it's an
internal, departmental script, and the end user's won't be putting cruft in
the command line option.

Regards,
Scott


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nike air force one, air force 1, air force one low cut, air force one salewholeta@163.com Digital Photography 3 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1 lky52193@gmail.com Computer Support 0 01-17-2008 04:40 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM
EOL - scanning single-quoted string Ajay Python 0 08-04-2004 01:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57