http://www.perlmonks.org?node_id=996770

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 testRegex.pl 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".

Thanks!.

Replies are listed 'Best First'.
Re: Problem with regex passed to a hash
by LanX (Saint) 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 (Archbishop) 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!