Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Embedding command-line arguments within source code

by morelenmir (Beadle)
on May 20, 2018 at 17:55 UTC ( [id://1214951]=perlquestion: print w/replies, xml ) Need Help??

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

I am currently in the process of moving from EPIC and Eclipse to writing my Perl programmes with a straightforward text editor and then debugging with the native debugger. For me the two most useful features of EPIC were the ability to set a breakpoint visually by clicking in the left margin and also being able to specify the command-line arguments for a script.

So far as setting breakpoints goes I have learned it is possible to do effectively the same thing in a text editor by adding $DB::single = 1; at the position in the source where you want the debugger to halt. However I have not yet found a way of pre-specifying the command-line arguments for a script within the source code. Is this possible? I always use GetOpt::Long to unpack and access any passed arguments. Therefore one idea that strikes me is to manually set the contents of $ARGV[n] at the top of the source before any other code is run. Would this approach work?

Any suggestions or insight in this matter would be much apreciated!

"Aure Entuluva!" - Hurin Thalion at the Nirnaeth Arnoediad.

Replies are listed 'Best First'.
Re: Embedding command-line arguments within source code
by haukex (Archbishop) on May 20, 2018 at 18:04 UTC
    Therefore one idea that strikes me is to manually set the contents of $ARGV[n] at the top of the source before any other code is run. Would this approach work?

    Sure, that's fine, just make sure to only set it when you're running under the debugger, so as to not override any command line options later set by the user. There isn't really anything that magical about @ARGV, most functions and modules only read it at runtime, and you can write to it whenever you like. If in theory you had a module that accessed @ARGV when you use it, you'd need to set it in a BEGIN block before you use that module, but I haven't come across a module that does this yet.

    use Data::Dump; use Getopt::Long; @ARGV = qw/ --foo --bar=123 --quz=Hello /; GetOptions(\my %OPTS1, 'foo|f', 'bar|b=i', 'quz|q=s') or die; dd \%OPTS1; @ARGV = qw/ -q World /; GetOptions(\my %OPTS2, 'foo|f', 'bar|b=i', 'quz|q=s') or die; dd \%OPTS2; __END__ { bar => 123, foo => 1, quz => "Hello" } { quz => "World" }
Re: Embedding command-line arguments within source code
by Discipulus (Canon) on May 20, 2018 at 18:36 UTC
    Hello morelenmir

    > Any suggestions..

    If with any you really mean any, you can find Modules as configuration files interesting.

    I still use it if I have a lot of arguments for a perl program or, let say, find command.

    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.

      Many thanks guys!!! That idea of using a module to input arguments is pretty clever all on its own. I will definitely give that a shot.

      Generally, learning to use the command-line debugger is a pretty slow and uncomfortable process at the moment. I aimed to work through the book 'Pro Perl Debugging' by Foley and Lester, but I found it quickly became rather confusing. However I am trying to extract the key information from the first few chapters and focus on just one or too commands--'x' to show a variable's value, 'c' to jump to the next breakpoint, 's' to step into and 'n' to step over.

      It would be nice to be able to customize the debugger appearance--set it for more than 80 columns for instance and specify what information it gives me at each step/jump. That is probably material for a different thread though!

      "Aure Entuluva!" - Hurin Thalion at the Nirnaeth Arnoediad.
Re: Embedding command-line arguments within source code
by LanX (Saint) on May 21, 2018 at 12:24 UTC
    I don't understand the connection between breakpoints in the debugger and command-line options. (?)

    I assume you might be interested to know that

    • The debugger is reading a config file, you can define breakpoints there
    • You can also define debugger commands like b 100 in the command line at start-up of the debugger to set a breakpoint at line 100
    • You can define your own "alias" command which interactively reads and sets breakpoints.

    see also perldb , perldbguts and DB ...

    ... and of course the h cmd:

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

Re: Embedding command-line arguments within source code
by ikegami (Patriarch) on May 20, 2018 at 22:05 UTC

    EPIC allows you to specify the arguments to provide to the script. "With EPIC 0.4.x or 0.5.x you should create a launch configuration of type Perl Local (under Run/Run...) and enter the arguments on the Arguments tab."

      read the first paragraph again

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found