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

Dear All I am trying to pass a file location in a subroutine to perform a file operation but I am unable to do so. Please have a look I am providing the code.
package alter; sub onPerform { my $nec = shift; my $ipic1 = org::item::essential($nec, "Input Picture"); my $(ipic) = $nec->locateAbsolutepath($ipic1);
How to call the method and pass the file location for "Input Picture".

Replies are listed 'Best First'.
Re: Passing file location in a subroutine
by Discipulus (Canon) on Oct 09, 2015 at 07:11 UTC
    welcome Rapazzini.. but ???

    First have a look at Markup in the Monastery to put code tags around your code.
    After this, your code does not make much sense to me. what org::item::essential is? a subroutine of the org::item class? what that (unknown to me) sub does? Normally a file path form depend on the underlaying operating system:
    my $linux_path = '/usr/bin/perl'; my $win_path = 'c:\scripts\my.log'; my $old_macos = 'Hard Drive:My Folder:My Document'; #dunno
    You have to pay attention when you pass file names to subs to the current directory where the program works. if you run your program in the /some/path and you do not change current directory while running, then a bare_filename.ext will be relative to /some/path ending to be /some/path/bare_filename.ext

    To deal with file path in a OS portable manner you can use the core module File::Spec as simply as:
    $path = File::Spec->catfile( @directories, $filename );

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thank you for the support. Let me clear the thing I have placed the .pm file code here.I now will try to explain it what I understood. I want to call it by using .pl where I will use the pic.pm file
      package alter; sub onPerform { my $nec = shift; my $ipic1 = org::item::essential($nec, "Input Picture"); my $(ipic) = $nec->locateAbsolutepath($ipic1);
      "org::item::essential" is a module of to get some parameters and the "input picture" have to get the parameters that will be assigned to $ipic1. The $(ipic) will take the $ipic1. I want to call the subroutine "onPerform" to input the "Input Picture"
      #!/usr/bin/perl -w use pic;#.pm file my $rapaz1 = 'C:/Users/RAPAZ/Desktop/try/train19_01.chm'; my $rapaz = alter->onPerform(); $rapaz-> {"Input Picture"} = ($rapaz1);
      Please help me how to call the subroutine and pass the file location "input picture" so that it gets the parameter and get prepared for $(ipic)

        First have a look at Markup in the Monastery to put code tags around your code.
        ...
        If you not help my old eyes there is no chance my old brain can produce something useful...

        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Passing file location in a subroutine
by kcott (Archbishop) on Oct 10, 2015 at 04:04 UTC

    G'day Rapazzini,

    Welcome to the Monastery.

    We are more than happy to help you; however, in order to do that, you need to provide appropriate information for us to do that. Telling us "I am unable to do so" gives us absolutely no clue as to what problem you have encountered. We have a page that answers the question "How do I post a question effectively?". Please read it.

    When politely asked to add code tags to make your post more readable, the appropriate course of action is not to post an even larger tract of hard-to-read text. Perhaps you didn't know how to add code tags but you could've asked. We have a page that answers the question "How do I change/delete my post?". Please read it.

    You clearly have little knowledge of Perl. That's fine: we all started with zero knowledge of Perl. I recommend you read "perlintro -- a brief introduction and overview of Perl". If you follow the advice therein, you'll at least be able to write Perl code that advises you of syntax errors and other real (or potential) problems. Here's an example of the type of feedback you might get (using one of the statements you posted):

    $ perl -we 'my $(ipic) = $nec->locateAbsolutepath($ipic1);' Bareword found where operator expected at -e line 1, near "$(ipic" (Missing operator before ipic?) Unquoted string "ipic" may clash with future reserved word at -e line +1. Can't use global $( in "my" at -e line 1, near "my $(" syntax error at -e line 1, near "$(ipic" Execution of -e aborted due to compilation errors.
    "How to call the method and pass the file location ..."

    I recommend you read "perlootut - Object-Oriented Programming in Perl Tutorial". By the time you've read the first few screenfuls, you should have a basic answer to this.

    For I more complete treatment of the subject, take a look at "perlobj - Perl object reference".

    As I said at the start, we're happy to help; however, the onus is on you to provide us with sufficient information such that we can do that.

    — Ken

      Thank you for the support and valuable suggestions.
Re: Passing file location in a subroutine
by u65 (Chaplain) on Oct 09, 2015 at 11:03 UTC