basic perl question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am going through a perl program, i have following lines which I dont
understand, please explain what those lines does and why.

if (/^$hash/) {
   $name="not accessible";

Re: basic perl question

On 29/10/12 20:01, vis29 wrote:
Quoted text here. Click to load it

You'll find this behaviour documented in "perldoc perlretut" (or however
you get at the built-in documentation on your machine); you'll find full
details there.

But in summary the stuff between // is a regular expression (regex), and
if no variable is specified to be matched against the regex then the
built-in variable $_ is assumed. It's equivalent to this:

   if ($_ =~ /^$hash/) {

Presumably you know what $hash is since it's a fundamental part
of the Perl language, but bear in mind that the hash key in the curly
brackets doesn't need to be in quotes if it's just alphanumeric.  In
this case it's equivalent to $hash.


Henry Law            Manchester, England

Re: basic perl question

Quoted text here. Click to load it

On top of what others have said, this code is probably wrong. The
pattern match should probably be /^\Q$hash/.

(Oh, and %hash should be called something more sensible, unless this is
a marijuana quality monitoring system.)


Site Timeline