Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Pre-evaluate string parameter

by shiftee (Initiate)
on Oct 20, 2011 at 14:38 UTC ( [id://932685]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I am trying to call a function using an interpolated string as the parameter.
Can it be done like this or do I need to stick an eval around it or something like that??

my $dirname = "/experiments/$experiment_name"; Fyp::Security::log( "Creating dir $dirname\n" );

I am using strict

Thanks,
Mark

Replies are listed 'Best First'.
Re: Pre-evaluate string parameter
by Eliya (Vicar) on Oct 20, 2011 at 15:27 UTC

    Sure, you can do it like this, no need for eval here.

    What happened when you tried?

    #!/usr/bin/perl -w use strict; package Fyp::Security; sub log { my $arg = shift; print $arg; }; package main; my $experiment_name = 'foo'; my $dirname = "/experiments/$experiment_name"; Fyp::Security::log( "Creating dir $dirname\n" );

    Interpolation - just like concatenation, i.e. "Creating dir " . $dirname . "\n" - happens at runtime, so you can arbitrarily "chain" interpolations  (if that's what your question is...)

Re: Pre-evaluate string parameter
by shiftee (Initiate) on Oct 20, 2011 at 16:34 UTC

    Oh, damn, I had defined the log function with empty parentheses like so:

    sub log() { ... }

    Theres no helping a true idiot,
    Thanks!!

          (original post)
      I am trying to call a function using an interpolated string as the parameter.
      Can it be done like this ...?
      I am using strict ...

         
      (subsequent post)
      ... I had defined the log function with empty parentheses ...

      shiftee: If you had originally posted the error message reported by Perl (and the code around-about the indicated line number), the humble monks might have avoided a dead-end side-trip along the path to giving you a pertinent answer to your question.

      >perl -wMstrict -le "sub foo () { print 'hi, i am foo'; } ;; main::foo('this is an argument'); " Too many arguments for main::foo at -e line 1, near "'this is an argum +ent')" Execution of -e aborted due to compilation errors.

      Short version: Don't use prototypes.

      Longer version: Prototypes don't do what you think they do so you shouldn't use them until you have read the perlsub documentation and really understand what's going on. Then you will know that most the time prototypes are not what you want.

      True laziness is hard work
Re: Pre-evaluate string parameter
by aartist (Pilgrim) on Oct 20, 2011 at 14:43 UTC
    You might just want to validate $dirname.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-24 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found