-1

I have some perl code (don't focus on what it does right now, i'm still working on it):

#!/usr/bin/perl -w

if(!@ARGV)
{
  print ("ERROR: NO ARGS\n");
  exit 1;
}

else
{
  use Cwd qw(cwd);
  my $dir = cwd;

  print $ARGV[0];
  @allfiles = glob($ARGV[0]);

  foreach $file (@allfiles){
    print "$file\n";
  }
}

On the line @allfiles = glob($ARGV[0]); the contents of ARGV[0] are being interpreted. I don't want them to be. For example, if ARGV[0] is *.txt, it will just interpret the metacharacter. I don't want it to do that. I want it to use "*.txt" as the input for glob(); How do I do this?

closed as unclear what you're asking by mosvy, Michael Homer, Mr Shunz, Sparhawk, telcoM Apr 28 at 8:22

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    How are you running the command? What is your argument list? – Inian Apr 24 at 4:36
  • If you do not want $ARGV[0] to be interpreted as a glob pattern, then why are you passing it to glob? write simply @allfiles = $ARGV[0]. – mosvy Apr 24 at 5:06
  • 4
    Show how you call this Perl script from the command line. – Kusalananda Apr 24 at 5:07
2

This is the one case where I suggest using bash aliases rather than functions. The bash manual says For almost every purpose, aliases are superseded by shell functions, this is the exception.

Normally if you say

 perl script *.txt

then the shell will expand the *.txt. This can be turned off using 'set +f'. The problem with using a function is that by the time the function is invoked, the shell has already expanded the pattern, so the trick is to use a combination of an alias and a function.

alias script="set +f ; _script"
_script(){ set -f ; perl script "$@" ; }

Then running

 script *.txt

will be processed as "set +f ; _script *.txt", so the pattern expansion is turned off before the _script function is invoked. The function turns pattern expansion back on, and then invokes the perl script.

The other way to avoid the issue is to use shell quoting, and say

  perl script '*.txt'

and the quotes will prevent the shell from doing the pattern expansion.

1

With the zsh shell, you can use:

alias myscript='noglob myscript'

noglob is a shell keyword that prevents globbing in the arguments (not redirections if multios is on) of the following simple command.

Note that you can still do

ENV_VAR=value myscript args

or

<input myscript args

Or you can simply use

myscript "*.$ext"

If you want the $ext to be expanded, but not the glob.

Not the answer you're looking for? Browse other questions tagged or ask your own question.