http://www.perlmonks.org?node_id=1083655

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

Hello Monks!

I am using PUBCHEM's PUG REST service to download properties of a specific chemical compound. I want to give STDIN input of the chemical compound in the form of a SMILES string (it's a format to represent a chemical compound, like a molecular formula). The PUG REST URL format looks like this:

http://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/CCC/property/MolecularFormula,MolecularWeight,TPSA/CSV/

http://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/<smiles string>/property/<properties of interest>/<output file format>/

Now, how do I STDIN my smiles string in the above format of URL? I've tried some little tricks like this:

#!/usr/bin/perl use LWP::Simple; my $smiles = <STDIN>; my $url = 'http://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/smiles/'; + my $finalurl = get "$url$smiles/property/MolecularFormula,MolecularWei +ght,TPSA/CSV/"; print $finalurl;
and warning was: Use of uninitialized value $finalurl in print at test_lwp.pl line 12. Thanks in advance :)

Replies are listed 'Best First'.
Re: How to use STDIN in between a URL?
by ikegami (Patriarch) on Apr 24, 2014 at 18:10 UTC
    You didn't remove the line terminator.
    chomp($smiles);

    You should at least have printed "$url$smiles/property/MolecularFormula,MolecularWeight,TPSA/CSV/" when trying to find the problem. And it would have found it.

      Oops! Thanks @ikegami :)
Re: How to use STDIN in between a URL?
by InfiniteSilence (Curate) on Apr 24, 2014 at 18:11 UTC

    chomp is your friend.

    DB<1> p $smiles CCC DB<2> chomp $smiles DB<3> p $smiles CCC DB<4> n main::(monks1083655.pl:8): my $finalurl = get "$url$smiles/property +/MolecularFormula,MolecularWeight,TPSA/CSV/"; DB<4> n main::(monks1083655.pl:9): print $finalurl; DB<4> p $finalurl "CID","MolecularFormula","MolecularWeight","TPSA" 6334,"C3H8",44.095620,0.0 DB<5> q

    Celebrate Intellectual Diversity

      Thanks, my friend :)