OVH Cloud OVH Cloud

Easy question - Beginner like - not answering forbidden ...

4 réponses
Avatar
Milca
The easiest question of the week (for you, not yet for me ...) :

I want to make in Perl an equivalent of the awk command-line :

awk -v var=value -f scrip_which_uses_var

... so as to be able to call easily my script with a simple - and single
- command line.

In Perl, how can I assign a value to a variable on the same command-line
which starts a script, allowing this script to use this variable ...???

I've tried a : perl -e "$var=value" script.pl but with no success.

I know, I'm new, that's THE sin.

Any help welcome ..!

Thank's by advance,

Micla.

4 réponses

Avatar
nicolas //
command line: perl script.pl arguments
arguments are passed to the script in a array named @ARGV.

eg, you can then do (in the script):

foreach (@ARGV) {print $_ . "n"} # prints arguments to STDOUT

I gess you should read a tutorial.


Le Thu, 02 Jun 2005 15:50:02 +0200
Milca a écrit:


The easiest question of the week (for you, not yet for me ...) :

I want to make in Perl an equivalent of the awk command-line :

awk -v var=value -f scrip_which_uses_var

... so as to be able to call easily my script with a simple - and single
- command line.

In Perl, how can I assign a value to a variable on the same command-line
which starts a script, allowing this script to use this variable ...???

I've tried a : perl -e "$var=value" script.pl but with no success.

I know, I'm new, that's THE sin.

Any help welcome ..!

Thank's by advance,

Micla.




--
nicolas //

Avatar
Michel Rodriguez
Milca wrote:

The easiest question of the week (for you, not yet for me ...) :

I want to make in Perl an equivalent of the awk command-line :

awk -v var=value -f scrip_which_uses_var

... so as to be able to call easily my script with a simple - and single
- command line.

In Perl, how can I assign a value to a variable on the same command-line
which starts a script, allowing this script to use this variable ...???

I've tried a : perl -e "$var=value" script.pl but with no success.



man perlrun ou perldoc perlrun donne:

-s enables rudimentary switch parsing for switches on the command line
after the program name but before
any filename arguments (or before an argument of --). This
means you can have switches with two lead‐
ing dashes (--help). Any switch found there is removed
from @ARGV and sets the corresponding variable
in the Perl program. The following program prints "1" if
the program is invoked with a -xyz switch,
and "abc" if it is invoked with -xyz«c.

#!/usr/bin/perl -s
if ($xyz) { print "$xyzn" }

donc perl -s -var«c script.pl

Ceci dit c'est pas vraiment recommende, et pour du code maintenable, il
est bien plus propre d'utiliser les modules qui parsent les options de
la ligne de commande, Getopt::* (Getopt::Std pour des options simples (1
lettre, a-la-unix-classique), Getopt::Long pour des options plus
complexes (a-la-gnu: --option) ou autre, je suis sur que chacun ici a
son module fetiche ;--)

--
mirod

Avatar
Milca
Michel Rodriguez wrote:

man perlrun ou perldoc perlrun donne:

-s enables rudimentary switch parsing for switches on the command line
after the program name but before
any filename arguments (or before an argument of --). This
means you can have switches with two lead‐
ing dashes (--help). Any switch found there is removed from
@ARGV and sets the corresponding variable
in the Perl program. The following program prints "1" if
the program is invoked with a -xyz switch,
and "abc" if it is invoked with -xyz«c.

#!/usr/bin/perl -s
if ($xyz) { print "$xyzn" }

donc perl -s -var«c script.pl



Test effectué, "perl -s -var«c script.pl", ça marche pas !!!!!

Mais ... "perl -s script.pl -var«c" ... ça marche !!!

L'informatique n'a pas fini de m'étonner ...

Thank's a lot pour votre aide !

Micla.

Avatar
Paul Gaborit
À (at) Fri, 03 Jun 2005 15:45:36 +0200,
Milca écrivait (wrote):
Test effectué, "perl -s -var«c script.pl", ça marche pas !!!!!


Normal : le "rudimentary switch parsing" dont parle la doc ne s'applique
qu'aux switchs passés *après* le nom du script :

-s enables rudimentary switch parsing for switches on the
command line *after* the program name but before any
filename arguments (or before an argument of --).

ou (en français) :

-s active une analyse rudimentaire des arguments sur la ligne de
commande situés *après* le nom du programme mais avant tout nom de
fichier passé en argument


Mais ... "perl -s script.pl -var«c" ... ça marche !!!


Et donc là, ça marche puisque -var«c est *après* script.pl !

--
Paul Gaborit - <http://perso.enstimac.fr/~gaborit/>
Perl en français - <http://perl.enstimac.fr/>