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

Hey guys, I am trying to run a system command on a string extracted from an sql database. The string appears to be extracted correctly (it prints fine), however whenever I try an run an external program on it (using system), the program only receives the first character. I have posted the code I am using below.
#!/usr/bin/env perl use strict; use warnings; use DBI; my $file="sample"; my $dbh=DBI->connect("dbi:SQLite:dbname=$file","","") or die "Unable t +o establish connection to $file\n"; my ($link)=$dbh->selectrow_array("SELECT value FROM ItemTable WHERE ke +y='link'"); print "\$link is $link\n"; system("echo", $link); $dbh->disconnect;
A sample sql file can be found at http://qpls.devio.us/sample Any help would be much appreciated,

Thanks,
Hermes

Replies are listed 'Best First'.
Re: system function not handling string properly
by choroba (Cardinal) on Sep 06, 2013 at 21:02 UTC
    Can you show what $link contains? It seems to work pretty well with echo, the only problem on my system is if $link is one of -n, -e, -E, or their combination (e.g.-eEn), which are interpreted as options to bash's builtin echo.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      $link does not contain any such characters. If you run the script on the sample sql file I linked to, the result is "www.youtube.com/watch?v=mark"
        Try running a hexdump on the string. It seems it is UTF-16 encoded, there is a zero byte after each character. echo probably takes the null char as the end of string as in C.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Actually, since system is called correctly, the shell won't be invoked and it'll be the real echo(1) which only parses the -n option (AFAIK). Anyway, it's better to use something like printf(1) since it tends not to have surprising behaviour: system('printf', '%s', $link);
Re: system function not handling string properly
by McA (Priest) on Sep 06, 2013 at 21:02 UTC

    Hi,

    what's about simply inserting a

    print Dumper($link);

    to show us what you get from the database?

    McA

      The script includes a print statement for this purpose. The print statement prints the contents of $link correctly, but system("echo", $link) only prints w. If you download the sample file I included and put it in the same directory of the script you can see the result yourself.

        Excuse me, I've overlooked that line.

        What is your OS? What shows 'which echo' on your system?

        This code works pretty well on my system:

        #!/usr/bin/env perl use strict; use warnings; use 5.010; my $url = '"www.youtube.com/watch?v=mark"'; system('echo', $url); $url = "www.youtube.com/watch?v=mark"; system('echo', $url);

        McA