Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Trying to understand some body's old code.

by akm2 (Scribe)
on Feb 27, 2001 at 20:20 UTC ( [id://61089]=perlquestion: print w/replies, xml ) Need Help??

akm2 has asked for the wisdom of the Perl Monks concerning the following question:

From what I can tell $HTMLLine =~ s/\$([a-zA-Z_0-9\[\]{}']+)/${$1}/g; was a cheap attempt to display $HTMLLine content instead of a $HTMLLine name and make it work with scalars, scalar arrays and hashes.

But when I try this little piece of code and print $HTMLLine I get a line is blank.

Any ideas why it doesnt work?

Replies are listed 'Best First'.
Re: Trying to understand some body's old code.
by arturo (Vicar) on Feb 27, 2001 at 20:35 UTC

    Well, for one thing, what pops up is going to depend on what's in $HTMLLine to begin with. (which is a roundabout way of asking what your data is!). What's the input? Does it come from an HTML file?

    Let's take a look at the regex and see if we can't tease out what the regex will do (whatever it was *intended* to do, we don't know ... but we might get a better idea by finding out what it does do!)

    • s/\$ match a literal "$" character, followed by ...
    • ([a-zA-Z_0-9\[\]{}'+) one or more of any letter, digit, underscore, [, ], {, }, or ', and store the results in $1
    • /${$1}/ REPLACE that with with the contents of $1 enclosed by curlies with a literal $ at the front.

    For example, this regex will replace each occurrence of something like $foo_bar with ${foo_bar}

    Looking at it, the regex is designed to allow you to encase Perl variable names in curlies (but it won't quite do that, because the single quote isn't a valid part of a Perl variable name ... unless this is an oddity of the fact that colons in package names get translated to single quotes? I'm fuzzy on such details, wait for guru guidance on that part of the issue ...); you would usually want to do this inside of double-quote contexts, because

    print "$foo_bar"
    Will print the contents of he variable $foo_bar, but what if you wanted to print the contents of $foo followed by the literal "_bar"? You could use concatenation, but then you're just asking for syntax errors and your code's looking messy. So Perl allows you to enclose the actual name of the variable in curlies, e.g.
    print "${foo}_bar"
    Will print out the contents of $foo followed by the literal "_bar".

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

(tye)Re: Trying to understand some body's old code.
by tye (Sage) on Feb 27, 2001 at 21:36 UTC

    Well, it wouldn't work for things other than scalars. To make it work, change it to:

    s/(\$[\w\[\]{}']+)/$1/gee;
    It wouldn't work because ${array[0]} isn't the same as $array[0]. This is a bit dangerous and still won't handle all cases.

    Update: Well, ${array[0]} actually is the same as $array[0]. My replacement also works OK, but they both fail for strings like q(${name}'s).

    Update: I was thinking that the FAQ had a method using eval which is pretty dangerous. I checked and that was mentioned in a post here, not in the FAQ. But that method has another problem in that it is hard to pick a delimiter to use in eval '"'.$str.'"' (since $str might contain a " or any other delimiter you pick).

    Well, it appears that my solution comes close to dealing with these problems, so I made it more robust:

    s/(\$[\w\[\]{}']+)/'"'.$1.'"'/gee;
    Note that I won't certify this as "safe" because I'm not convinced that someone more clever than I can't come up with a string that would do bad things like reboot your computer (like ${[system'shutdown']} or ${\qx'shutdown'} almost do) or just that fails to be interpretted properly.

            - tye (but my friends call me "Tye")
Re: Trying to understand some body's old code.
by azatoth (Curate) on Feb 27, 2001 at 20:29 UTC
Re: Trying to understand some body's old code.
by eg (Friar) on Feb 27, 2001 at 20:38 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://61089]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2024-04-25 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found