2

I am writing a large Perl script, which needs to utilize other existing Perl scripts. The problem is the main script needs to reference many different scripts from different folders. For example the main script would be contained in:

/perl/programs/io 

It may need to run a script which is stored in:

/perl/programs/tools 

Note that there are other orthogonal folders besides tools so I need to be able to access any of them on the fly.

Currently this is what I got:

my $mynumber = '../tools/convert.pl bin2dec 1011';

In theory it should move back from the io directory then enter the appropriate tool directory and call the convert.pl script while passing it the parameters.

All this does is store the string in the single quotes to $myNumber.

5
  • 5
    Uhhh... you're just assigning a string to a variable. Perhaps you meant to use backticks? Commented Jan 27, 2016 at 21:34
  • 3
    Possible duplicate of How do I run a Perl script from within a Perl script? Commented Jan 27, 2016 at 21:36
  • @MattJacob yes, but the accepted answer there is a bit overkill for this case. Commented Jan 28, 2016 at 10:37
  • @simbabque The accepted answer, perhaps. But there are other answers within that thread that do answer this question (and the question is the same). Commented Jan 28, 2016 at 17:15
  • @m, I agree, but my vote would hammer it, so I didn't. Commented Jan 28, 2016 at 18:24

3 Answers 3

2

I like to assign the output of a command to an array so I can loop through the array to find error or other messages. For example if I'm making a zip file to email to someone I want to check to see if the zip program had any errors before I continue to make and send the email.

@msgs = `zip -f myfile.zip *.pl`; # Use backticks

You can also assign the output to a scalar:

$msg = `ls -al *.pl`; # Use backticks
Sign up to request clarification or add additional context in comments.

Comments

2

To run any system command or script all you have to do is use `backticks`. From observing another programer's perl code, I misread these strange quotes for 'single quotes'.

backticks are also nice because they return the text in STDOUT to your perl script so that the output can be assigned to a variable, something I have found impossible if using system("");

Comments

1

The similar question answer does not work with my version of perl. The line

use IPC::System::Simple qw(system capture);

throws some errors. However just using system works, like this:

my $mynumber = system($^X, "../tools/convert.pl", 'bin2dec', '1011');

I can use the above without setting equal to something to execute scripts which return no value and are only sent arguments.

This seems to be the easiest way to do what I need to and the entire programs folder can be moved anywhere and it will still work as no parent directories above programs are used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.