Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Problem with regex passed to a hash

by oldwarrior32 (Sexton)
on Oct 01, 2012 at 23:52 UTC ( #996770=perlquestion: print w/replies, xml ) Need Help??
oldwarrior32 has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I ask for your wisdom again dear monks.

I am trying this:

my $username = 'john.doe'; $username = quotemeta($username); ($prematch,$match) = $session->waitfor(match => '/$username/', Timeout => 10);

But I got this error:

Global symbol "$username" requires explicit package name at (eval 11) line 1, <> l ine 247. ...propagated at line 128, <> line 247.

It looks like the method is interpreting that I'm passing a variable to the regex, and not a string. I really need to pass the variable, but only the string inside the variable is what the method should get.

The waitfor method comes from the Telnet module.

How do I just pass the string and not the variable?

The idea behind this, is that I got a lot of username to test, and all of them uses the format "name.surname".


Replies are listed 'Best First'.
Re: Problem with regex passed to a hash
by LanX (Bishop) on Oct 02, 2012 at 00:12 UTC
    did you try doublequotes instead of singlequotes?

    Cheers Rolf

      Thanks very much! It worked.

      It looks like with single quotes I passing the regex as is, with no variable interpolation.

        > It looks like with single quotes I passing the regex as is

        not exactly. your passing the variable's symbol.

        Seems like that module does an eval on the string you pass.

        So passing in singelquotes means that eval '/$username/' fails because $username is unknown in the module's namespace.

        Cheers Rolf

Re: Problem with regex passed to a hash
by toolic (Bishop) on Oct 02, 2012 at 00:16 UTC
    Try qr:
    ($prematch,$match) = $session->waitfor(match => qr/$username/, Timeout => 10);

      With qr doesn't appears the error, but there is no match(?)

      Gonna study the qr function. Thanks!

        Yeah, Net::Telnet is rather old and uses a weird method of passing regexes as arguments. For a more recent module qr// would be appropriate, but Net::Telnet requires a string that starts and ends with a slash.

        The other posters are right when they say you should double quote. Any method for building strings will work:

        '/'.$username.'/' "/$username/" sprintf('/%s/', $username)
        As an aside, if you're trying to log in, there's a login method in Net::Telnet.
Re: Problem with regex passed to a hash
by kcott (Chancellor) on Oct 02, 2012 at 00:22 UTC

    G'day oldwarrior32,

    I suspect you want double-quotes around $usename.

    I had intended to investigate this a little more thoroughly but I can't find a Telnet module on CPAN - there's quite a few with Telnet as part of their names. Perhaps provide a link to the module in question.

    -- Ken

      The module is Net::Telnet

      As you mention, double quotes does the job. Thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996770]
Approved by LanX
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-06-25 15:59 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (127 votes). Check out past polls.