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

hi,
I have the following bash-script where I would like to get rid of the call to head.
The problem is simply to return the first match to a reg-ex but I seem to be too drunk to do it....

Any takers?

#!/bin/bash master=$(pacmd list-sinks | perl -ne 'print "$1\n" if /name: <(.*)>/' +| head -n 1) pacmd load-module module-remap-sink sink_name=mono master=$master chan +nels=2 channel_map=mono,mono

Replies are listed 'Best First'.
Re: improve script
by choroba (Cardinal) on Nov 27, 2015 at 06:01 UTC
    What about exiting after the print?
    print "$1\n" and exit if /.../
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      print "$1\n" and exit if /.../

      Funny to read, but not funny when printing fails for some reason:print and exit exists ONLY if print was successful.

      >cat print-and-exit.pl #!/usr/bin/perl use strict; use warnings; close STDOUT if @ARGV; 1 and print "foo" and exit 0; die "Ooops, still here"; >perl print-and-exit.pl foo >perl print-and-exit.pl kaboom print() on closed filehandle STDOUT at print-and-exit.pl line 8. Ooops, still here at print-and-exit.pl line 9. >

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        I must be too ignorant to see why STDOUT should be closed in a one-liner (???)

        Anyway it's true is that chaining with and is a lazy way to avoid the already mentioned scalar comma operator:

          print, exit if cond

        This seems to confuse many people, but has the same effect as

          do {print; exit} if cond

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

        So, what's your solution?
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      :D  /(...)/ and print ... and exit