in reply to Re^4: Getting File Types data
in thread Getting File Types data

it would be good if someone could explain how the follwing two Perl lines work since I do not understand them.
my ( $extention, $type ) = split '=', `assoc $arg_item 2>nul`; my( undef, $cmd ) = split '=', `ftype $type 2>nul`;

Essentially both lines do the same thing for the two commands. Breaking those lines down

  1. `cmd $arg 2>nul`

    Where cmd is either assoc or ftype; $arg is $arg_item or $type; and the 2>nul is command shell (cmd.exe) redirect of stderr to the nul device.

    The backticks (``) execute the command given passing the argument ($arg_item or $type respectively), and retrieve the output from those commands, whilst discarding any error messages produced.

    The output from both commands is in a similar format:

    c:\test>assoc .pl .pl=Perl c:\test>ftype Perl Perl="c:\perl\bin\perl.exe" -sw "%1" %*

    Ie. something=this other thing and the bit you want in both cases is the bit after the = sign.

  2. split '=', `...`; so the output of the command is past directly to split which splits the output into two pieces either side of the = and discard the =.
  3. my( $var1, $var2 ) = ... assigns names to those two pieces.

    Except that we don't really need the first part as that is just the same as the argument we gave the command. So in one case I chose not to name that part:

    my( undef, $cmd ) = split '=', `ftype $type 2>nul`;

    The undef there simply says that I don't really need to name the first part, so it is effectively discarded.

    Equally, the first line could also do the same thing and become:

    my ( undef, $type ) = split '=', `assoc $arg_item 2>nul`;

    Since we never make use of the variable I named $extension.

Well done for asking. I hope my explanation clarifies things?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."